import { Service } from 'egg';
import { Evaluate } from '../model/Evaluate';

export default class EvaluateService extends Service {
    /**
     * 新增评论
     * @param param 
     */
    public async createEvaluate(param: any) {
        try {
            const employee_id = param.employee_id;
            const target_employee_id = param.target_employee_id;
            const result: any = await this.ctx.model.Evaluate.findOne<Evaluate>({ attributes: ['evaluate_id'], where: { employee_id, target_employee_id } });
            if (!result) {
                const submitData: any = {};
                submitData.evaluate_id = this.ctx.helper.genUUID();
                submitData.employee_id = param.employee_id;
                submitData.target_employee_id = param.target_employee_id;
                submitData.evaluate_content = param.evaluate_content;
                submitData.evaluate_picture = param.evaluate_picture;
                submitData.is_delete = '0';
                const createResult: any = await this.ctx.model.Evaluate.create(submitData);
                if (!createResult) {
                    throw new Error(JSON.stringify({ code: 2, msg: '评论失败' }));
                }
            } else {
                const evaluate_id = result.evaluate_id;
                const updateResult: any = await this.ctx.model.Evaluate.update({ evaluate_content: param.evaluate_content, evaluate_picture: param.evaluate_picture, is_delete: '0' }, { where: { evaluate_id } });
                if (updateResult[0] <= 0) {
                    throw new Error(JSON.stringify({ code: 2, msg: '评论失败' }));
                }
            }
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 删除评论
     * @param param 
     */
    public async deleteEvaluate(param: any) {
        try {
            const evaluate_id = param.evaluate_id;
            const deleteResult: any = await this.ctx.model.Evaluate.update({ is_delete: '1' }, { where: { evaluate_id } });
            if (deleteResult[0] <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '删除失败' }));
            }
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 查找部门职员及评价列表
     * @param param 
     */
    public async searchEvaluateList(param: any) {
        try {
            const { work_unit_id, department_id, employee_id } = param;
            const queryStr = `select temp1.employee_id,temp1.employee_name,temp1.employee_head_portrait,temp2.evaluate_id,temp2.evaluate_content,temp2.evaluate_picture from 
            (select employee_id,employee_name,employee_head_portrait,created_at from t_employee where work_unit_id=:work_unit_id and department_id=:department_id and is_delete='0' and employee_id not in (:employee_id)) temp1 
            left join (select evaluate_id,target_employee_id,evaluate_content,evaluate_picture from t_evaluate where employee_id=:employee_id and is_delete='0') temp2 on 
            temp1.employee_id=temp2.target_employee_id order by temp1.created_at DESC`;
            const result: any = await this.ctx.model.query(queryStr, {
                type: this.ctx.model.QueryTypes.SELECT,
                replacements: { work_unit_id, department_id, employee_id }
            })
            if (result.length <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '没有数据' }));
            }
            return result;
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 根据评论id查找评论内容
     * @param param 
     */
    public async searchEvaluateById(param: any) {
        try {
            const evaluate_id = param.evaluate_id;
            const result: any = await this.ctx.model.Evaluate.findOne<Evaluate>({ attributes: ['evaluate_content', 'evaluate_picture'], where: { evaluate_id, is_delete: '0' } });
            if (!result) {
                throw new Error(JSON.stringify({ code: 2, msg: '没有数据' }));
            }
            return result;
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 修改评论内容
     * @param param 
     */
    public async updateEvaluate(param: any) {
        try {
            const { evaluate_id, evaluate_content, evaluate_picture } = param;
            const result: any = await this.ctx.model.Evaluate.update({ evaluate_content, evaluate_picture }, { where: { evaluate_id } });
            if (result[0] <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '修改失败' }));
            }
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 根据用户id查找评论内容
     * @param param 
     */
    public async searchEvaluateByEmployeeId(param: any) {
        try {
            const target_employee_id = param.target_employee_id;
            const result: any = await this.ctx.model.Evaluate.findOne<Evaluate>({ attributes: ['evaluate_content', 'evaluate_picture'], where: { target_employee_id, is_delete: '0' } });
            if (!result) {
                throw new Error(JSON.stringify({ code: 2, msg: '没有数据' }));
            }
            return result;
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }
}